package com.block.channel.service.impl;

import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.block.db.entity.ChannelBaseInfo;
import com.block.db.entity.ChannelCountUvInfo;
import com.block.db.mapper.ChannelCountUvInfoMapper;
import com.block.channel.service.IChannelCountUvInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 * 渠道统计uv信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-09-01
 */
@Service
public class ChannelCountUvInfoServiceImpl extends ServiceImpl<ChannelCountUvInfoMapper, ChannelCountUvInfo> implements IChannelCountUvInfoService {

    @Async
    @Override
    public void saveUvCountInfo(ChannelBaseInfo channelBaseInfo, String randomId, String reqIp, String userAgent) {

        try {
            ChannelCountUvInfo channelCountUvInfo = new ChannelCountUvInfo();
            channelCountUvInfo.setDateInfo(LocalDate.now().toString());
            channelCountUvInfo.setChannelCode(channelBaseInfo.getChannelCode());
            channelCountUvInfo.setChannelName(channelBaseInfo.getChannelName());
            channelCountUvInfo.setAppCode(channelBaseInfo.getAppCode());
            channelCountUvInfo.setAppName(channelBaseInfo.getAppName());
            channelCountUvInfo.setReqIp(reqIp);
            channelCountUvInfo.setRandomId(randomId);

            UserAgent userAgentEntity = UserAgentUtil.parse(userAgent);
            channelCountUvInfo.setBrowserName(userAgentEntity.getBrowser().getName());
            channelCountUvInfo.setPlatformName(userAgentEntity.getPlatform().getName());

            this.save(channelCountUvInfo);
        }catch (Exception exception){
            exception.printStackTrace();
        }
    }
}
